package com.group02.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;


import com.group02.community.entity.community.ZyBuilding;
import com.group02.community.entity.community.ZyUnit;
import com.group02.community.entity.vo.queryVO.UnitQueryVO;
import com.group02.community.service.UnitService;
import com.group02.community.utils.EasyExcelUtil;
import com.group02.community.utils.ResponseResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@Api
@RequestMapping("/unit")
public class UnitController {

    @Autowired
    private UnitService unitService;

    @ApiOperation("获取单元的分页列表")
    @PostMapping("/pageList")
    public ResponseResult<IPage<ZyUnit>> getUnitPageList(Integer currentPage, Integer pageSize, String communityId, @RequestBody ZyUnit zyUnit) {
        IPage<ZyUnit> unitPageList = unitService.getUnitPageList(currentPage, pageSize, communityId, zyUnit);
        return ResponseResult.ok(unitPageList);

    }

    @ApiOperation("获取电梯状态")
    @GetMapping("/elevator")
    public ResponseResult<List<String>> getElevatorList() {
        List<String> elevatorList = unitService.getElevatorList();

        return ResponseResult.ok(elevatorList);
    }

    @ApiOperation("添加单元")
    @PostMapping("/add")
    public ResponseResult<String> addUnit(@RequestBody ZyUnit zyUnit) {
        unitService.save(zyUnit);
        return ResponseResult.ok("添加成功");
    }

    @ApiOperation("批量删除单元")
    @PostMapping("/deleteByIds")
    public ResponseResult<String> deleteUnitByIds(@RequestBody String[] ids) {
        System.out.println(ids);
        unitService.removeBatchByIds(Arrays.asList(ids));
        return ResponseResult.ok("删除成功");
    }

    @ApiOperation("导出")
    @PostMapping("/excel")
    public ResponseResult<String> exportUnitByIds(@RequestBody String[] ids, HttpServletResponse response) {

        List<ZyUnit> zyUnits = unitService.listByIds(Arrays.asList(ids));
        String fileName = UUID.randomUUID().toString();
        String path = "C:\\Users\\Administrator\\Desktop\\" + fileName + "单元信息表.xls";    //导出路径
        EasyExcelUtil.write(path, ZyUnit.class, zyUnits, 0, "单元信息表");
        return ResponseResult.ok("导出成功");
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ResponseResult<String> update(@RequestBody ZyUnit zyUnit) {
        unitService.updateById(zyUnit);
        System.out.println(zyUnit);
        return ResponseResult.ok("修改成功");
    }
    @ApiOperation("单删")
    @GetMapping("/delOne/{unitId}")
    public ResponseResult<String> delOne(@PathVariable String unitId) {
        unitService.removeById(unitId);
        return ResponseResult.ok("删除成功");
    }

    @ApiOperation("根据楼栋ID查单元")
    @GetMapping("/getZyUnitByBuildingId/{buildingId}")
    public ResponseResult<List<ZyUnit>> getZyUnitByBuildingId(@PathVariable String buildingId){
        return ResponseResult.ok(unitService.getZyUnitByBuildingId(buildingId));
    }




}
